// js para processar detalhes na tabela com a lista de endereços MAC.
var oTable;
var details = new Array();

/* Formating function for row details */
function fnFormatDetails ( nTr )
{
	var iIndex = oTable.fnGetPosition( nTr );
	//var aData = oTable.fnSettings().aoData[iIndex]._aData;
	var aData = details[iIndex];

	var sOut = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
	sOut += '<tr><td>Setor</td><td>'+aData[0]+'</td></tr>';
	sOut += '<tr><td>Responsável</td><td>'+aData[2]+'</td></tr>';
	sOut += '<tr><td>Sistema Operacional</td><td>'+aData[1]+'</td></tr>';
	sOut += '<tr><td>Faixa IP</td><td>'+aData[3]+'</td></tr>';
	sOut += '</table>';

	return sOut;
}

function Placa() {
	return this;
}

function addDetails(Setor, SO, Resp, Faixa) {
	var d = new Array(Setor, SO, Resp, Faixa);
	details.push(d);
}

$(document).ready(function() {
	/*
			* Insert a 'details' column to the table
			*/
	var nCloneTh = document.createElement( 'th' );
	var nCloneTd = document.createElement( 'td' );
	nCloneTd.innerHTML = '<img src="/madmac/icons/details_open.png" class="details">';
	nCloneTd.className = "center";
	//nCloneTh.textContent = "Detalhes";
	//nCloneTh.style = "width: 10%;";

	$('#example thead tr').each( function () {
		this.insertBefore( nCloneTh, this.childNodes[0] );
	});

	$('#example tbody tr').each( function () {
		this.insertBefore(  nCloneTd.cloneNode( true ), this.childNodes[0] );
	});

	/*
			* Initialse DataTables, with no sorting on the 'details' column
			*/
	oTable = $('#example').dataTable( {
		"aoColumns": [
		{
			"bSortable": false
		},
		null, null, null, null, null
		],
		"sPaginationType": "full_numbers",
		"aaSorting": [[1,'asc']]
	});

	/* Add event listener for opening and closing details
			* Note that the indicator for showing which row is open is not controlled by DataTables,
			* rather it is done here
			*/
	$('td img', oTable.fnGetNodes() ).each( function () {
		$(this).click( function () {
			var nTr = this.parentNode.parentNode;
			if ( this.src.match('details_close') )
			{
				/* This row is already open - close it */
				this.src = "/madmac/icons/details_open.png";
				oTable.fnClose( nTr );
			}
			else
			{
				/* Open this row */
				this.src = "/madmac/icons/details_close.png";
				oTable.fnOpen( nTr, fnFormatDetails(nTr), 'details' );
			}
		});
	});
});
